<?php
/**
 * City.class.php
 *
 * @author camfee <camfee@foxmail.com>
 * @date   18-1-2 下午9:17
 */

namespace Center;

use Common\ModuleBase;
use Common\Bridge;

class City extends ModuleBase
{
    protected static $_conf = [
        'db' => [
            'w' => Bridge::DB_CENTER_W,
            'r' => Bridge::DB_CENTER_R
        ],
        'table' => 'City',
        'fields' => [
            'CityId' => self::VAR_TYPE_KEY,
            'CityName' => self::VAR_TYPE_STRING,
            'ProvinceId' => self::VAR_TYPE_INT,
            'ProvinceName' => self::VAR_TYPE_STRING,
            'Status' => self::VAR_TYPE_INT,
            'UpdateTime' => self::VAR_TYPE_STRING,
            'CreateTime' => self::VAR_TYPE_STRING,
        ],
        'mc' => Bridge::MEMCACHE_DEFAULT,
        // 可选, MC KEY, "KeyName:%d", %d会用主键ID替代
        'mckey' => 'City:%d',
        // 可选, 超时时间, 默认不过期
        'mctime' => 86400,
    ];

    const MC_VALID_CITY_LIST = 'Valid_City_List';
    // 列表缓存
    protected static $_cache_list_keys = [
        self::MC_VALID_CITY_LIST => self::CACHE_LIST_TYPE_MC
    ];
    // 新增必须字段 field => 1
    protected static $_add_must_fields = [
        'Name' => 1,
    ];

    /**
     * 获取所有开通城市
     *
     * @return array
     */
    public static function getAllCity()
    {
        $extra = [
            'type' => parent::MOD_TYPE_MEMCACHE,
            'mckey' => self::MC_VALID_CITY_LIST,
            'mtime' => 86400,
            'fields' => '*',
            'listkey' => 'CityId',
        ];

        return parent::getDataByFields(['Status' => 1], $extra);
    }

    /**
     * 根据名称获取城市详细
     *
     * @param $name
     * @return bool|mixed
     *     [Division] => 430100
     *     [Name] => 长沙
     *     [FullName] => 长沙市
     *     [Pinyin] => ChangSha
     *     [Acronym] => CS
     *     [AreaCode] => 0731
     *     [ZipCode] => 410005
     */
    public static function getCityInfoByName($name)
    {
        static $city1, $city2;
        $areas = loadconf('geograph/area')['area'];
        if (empty($city1) || empty($city2)) {
            foreach ($areas as $v) {
                foreach ($v as $vv) {
                    $city1[$vv['Name']] = $vv;
                    $city2[$vv['FullName']] = $vv;
                }
            }
        }
        if (isset($city1[$name])) {
            return $city1[$name];
        }
        if (isset($city2[$name])) {
            return $city2[$name];
        }

        return false;
    }

    /**
     * 根据名称获取城市Id详细
     *
     * @param $city_id
     * @return bool|mixed
     *      [Division] => 430100
     *      [Name] => 长沙
     *      [FullName] => 长沙市
     *      [Pinyin] => ChangSha
     *      [Acronym] => CS
     *      [AreaCode] => 0731
     *      [ZipCode] => 410005
     */
    public static function getCityInfoById($city_id)
    {
        static $city1;
        $citys = loadconf('geograph/city')['city'];
        if (empty($city1)) {
            foreach ($citys as $v) {
                foreach ($v as $vv) {
                    $city1[intval($vv['Division'])] = $vv;
                }
            }
        }
        if (isset($city1[$city_id])) {
            return $city1[$city_id];
        }

        return false;
    }

    /**
     * @see ModuleBase::add() 新增
     * @see ModuleBase::update() 更新
     * @see ModuleBase::getInfoByIds() 按id查询
     * @see ModuleBase::getList() 条件查询
     * @see ModuleBase::delete() 删除
     */

}